let path = require('path');
let webpack = require('webpack');
let HtmlWebpackPlugin = require('html-webpack-plugin');

let isProduction = process.env.NODE_ENV === 'production';

let config = {
	mode: process.env.NODE_ENV,
	entry: {
		index: './src/index.js'
	},
	output: {
		path: '/dist',
		publicPath: '/',
		filename: '[name]-[hash:8].js',
		chunkFilename: '[name]-[chunkhash].js'
	},
	module: {
		rules: [
			{
				test: /\.js$/,
				include: path.join(__dirname, 'src'),
				exclude: [/node_modules/],
				use: [{
					loader: 'babel-loader',
					options: {
						cacheDirectory: true,
						presets: ['@babel/preset-env']
					}
				}]
			}, {
				test: /\.css$/,
				use: [
					'style-loader',
					'css-loader'
				],
			}]
	},
	plugins: [
		new webpack.optimize.OccurrenceOrderPlugin(),
	],
	// 使用webpack-dev-server，提高开发效率
	devServer: {
		proxy: {
			'/one': {
				'target': 'http://localhost:9001',
				'pathRewrite': {
					'^/one': ''
				},
				'changeOrigin': true,
				'secure': false
			},
			'/two': {
				'target': 'http://localhost:9002',
				'pathRewrite': {
					'^/two': ''
				},
				'changeOrigin': true,
				'secure': false
			},
			'/ws': {
				'target': 'ws://localhost:9001',
				'pathRewrite': {
					'^/ws': ''
				},
				'changeOrigin': true,
				'secure': false,
				port: 18474,
				ws: true
			},
		},
        contentBase: './',
        host: 'localhost',
        hot: true,
        port: 10808,
        compress: false, // 是否使用gzip压缩
        // noInfo: true, // 不显示不紧要的提示信息
        overlay: {// 在整个页面显示编译的错误
            warnings: true,
            errors: true
        },
        open: 'Google Chrome', // 自动打开的浏览器
        // openPage: 'login' // 自动打开的地址
	}
};

if (!isProduction) {
	//开发态才注入 agency4api
	// let agency4apiWebpack = require('agency4api/webpack/index.js');
	// let proxyConfig = agency4apiWebpack.getProxyConfig({
	// 	// 注入 agency4api 的路径，访问此路径后，会有相关信息注入的前端
	// 	insertUrl: '/login',
	// 	// 访问 insertUrl 后，跳转的目标路径
	// 	targetUrl: '/',
	// 	// 保存配置文件的目录的绝对路径
	// 	relativeFolder: path.resolve('./config')
	// });
	// let rules = agency4apiWebpack.getInsertRules([/src[\\/]index.js$/]);

	// config.module.rules.push(...rules);
	// config.devServer.proxy = proxyConfig.proxy;

    let agency4apiDemo = require('agency4api')('webpack');
    let proxyConfig = agency4apiDemo.getProxyConfig({
        proxy: config.devServer.proxy,
        initAgency4apiOptions: {
            ignoreQuery: ['t']
        }
    });
    let rules = agency4apiDemo.getInsertRules([/src[\\/]index.js$/]);

    config.module = config.module || {};
    // eslint-disable-next-line no-multi-assign
    let module_rules = config.module.rules = config.module.rules || [];
    module_rules.push(...rules);
    config.devServer.proxy = proxyConfig.proxy;

	config.devtool = '#eval-source-map'
}
config.plugins.push(
	new HtmlWebpackPlugin({
		// favicon: `${srcFolder}/images/logo.ico`,
		filename: './index.html', //生成的html存放路径，相对于path
		template: 'src/index.html', //html模板路径
		inject: 'body', //js插入的位置，true/'head'/'body'/false
		hash: true, //为静态资源生成hash值
		minify: { //压缩HTML文件
			removeComments: false, //是否移除HTML中的注释 不移除注释，因为模板语法可能会用到注释语法
			collapseWhitespace: false //是否删除空白符与换行符
		},
	})
);

module.exports = config;